<template>
  <div class="p-2">
    <div v-if="showSearch" class="mb-[10px]">
      <el-card shadow="hover">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="园区名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入园区名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="管理单位" prop="managementUnit">
            <el-input v-model="queryParams.managementUnit" placeholder="请输入管理单位" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="物业公司" prop="propertyCompany">
            <el-input v-model="queryParams.propertyCompany" placeholder="请输入物业公司" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>

    <el-card shadow="never">
      <template #header>
        <div style="text-align: right">
          <el-button type="warning" icon="Download" @click="handleExport">导出</el-button>
          <el-button plain icon="Plus" @click="handleOpenDialog('create')"> 新增 </el-button>
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"> 删除 </el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="foundationInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center">
          <template #default="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column label="园区名称" align="center" prop="name" />
        <el-table-column label="位置" align="center" prop="position" />
        <el-table-column label="占地面积(亩)" align="center" prop="area" />
        <el-table-column label="建筑面积(㎡)" align="center" prop="floorArea" />
        <el-table-column label="管理单位" align="center" prop="managementUnit" />
        <el-table-column label="物业公司" align="center" prop="propertyCompany" />
        <el-table-column label="物业人数" align="center" prop="numberOfProperties" />
        <el-table-column label="物业联系人" align="center" prop="propertyContacts" />
        <el-table-column label="物业电话" align="center" prop="propertyTelephone" />
        <el-table-column label="操作" align="center" width="120" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="编辑" placement="top">
              <el-button link type="primary" @click="handleOpenDialog('update', scope.row.id)">编辑</el-button>
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button type="primary" link @click="handleOpenDialog('detail', scope.row.id)">详情</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <FoundationInfo ref="foundationDialog" @success="handleSuccess" />
  </div>
</template>

<script setup name="FoundationInfo" lang="ts">
import { ref, reactive, toRefs, onMounted } from 'vue';
import { listFoundationInfo, getFoundationInfo, delFoundationInfo, addFoundationInfo, updateFoundationInfo } from '@/api/zichanguanli/foundationInfo';
import { FoundationInfoVO, FoundationInfoQuery, FoundationInfoForm } from '@/api/zichanguanli/foundationInfo/types';
import FoundationInfo from './form.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const foundationInfoList = ref<FoundationInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const foundationDetailDialog = ref();

const queryFormRef = ref<ElFormInstance>();
const foundationInfoFormRef = ref<ElFormInstance>();

const initFormData: FoundationInfoForm = {
  id: undefined,
  name: undefined,
  position: undefined,
  area: undefined,
  floorArea: undefined,
  managementUnit: undefined,
  contactPerson: undefined,
  associates: undefined,
  estimation: undefined,
  propertyCompany: undefined,
  numberOfProperties: undefined,
  propertyContacts: undefined,
  propertyTelephone: undefined,
  remark: undefined,
  attachments: undefined,
  introduce: undefined,
  images: undefined
};

const data = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    position: undefined,
    area: undefined,
    floorArea: undefined,
    managementUnit: undefined,
    contactPerson: undefined,
    associates: undefined,
    estimation: undefined,
    propertyCompany: undefined,
    numberOfProperties: undefined,
    propertyContacts: undefined,
    propertyTelephone: undefined,
    attachments: undefined,
    introduce: undefined,
    images: undefined,
    creatTime: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '园区主键标识不能为空', trigger: 'blur' }],
    name: [{ required: true, message: '园区名称不能为空', trigger: 'blur' }],
    position: [{ required: true, message: '位置不能为空', trigger: 'blur' }],
    area: [{ required: true, message: '面积(亩)不能为空', trigger: 'blur' }],
    floorArea: [{ required: true, message: '建筑面积不能为空', trigger: 'blur' }],
    managementUnit: [{ required: true, message: '管理单位不能为空', trigger: 'blur' }],
    propertyCompany: [{ required: true, message: '物业公司不能为空', trigger: 'blur' }],
    numberOfProperties: [{ required: true, message: '物业人数不能为空', trigger: 'blur' }],
    propertyContacts: [{ required: true, message: '物业联系人不能为空', trigger: 'blur' }],
    propertyTelephone: [{ required: true, message: '物业电话不能为空', trigger: 'blur' }],
    attachments: [{ required: true, message: '园区附件不能为空', trigger: 'blur' }],
    introduce: [{ required: true, message: '园区介绍不能为空', trigger: 'blur' }],
    creatTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

// 表单字段配置
const formFields = [
  { label: '园区名称', prop: 'name', type: 'text' },
  { label: '位置', prop: 'position', type: 'text' },
  { label: '面积(亩)', prop: 'area', type: 'text' },
  { label: '建筑面积', prop: 'floorArea', type: 'text' },
  { label: '管理单位', prop: 'managementUnit', type: 'text' },
  { label: '对接人', prop: 'contactPerson', type: 'text' },
  { label: '联系人', prop: 'associates', type: 'text' },
  { label: '园区估值', prop: 'estimation', type: 'text' },
  { label: '物业公司', prop: 'propertyCompany', type: 'text' },
  { label: '物业人数', prop: 'numberOfProperties', type: 'text' },
  { label: '物业联系人', prop: 'propertyContacts', type: 'text' },
  { label: '物业电话', prop: 'propertyTelephone', type: 'text' },
  { label: '园区备注', prop: 'remark', type: 'text' },
  { label: '园区介绍', prop: 'introduce', type: 'text' },
  { label: '园区附件', prop: 'attachments', type: 'file' },
  { label: '实景图片', prop: 'images', type: 'image' }
];

const handleDetail = (row: FoundationInfoVO) => {
  foundationDetailDialog.value.open(row);
};

/** 查询园区基础信息列表 */
const getList = async () => {
  loading.value = true;
  try {
    const res = await listFoundationInfo(queryParams.value);
    foundationInfoList.value = res.rows;
    total.value = res.total;
  } catch (error) {
    console.error('获取园区基础信息列表失败', error);
    proxy?.$modal.msgError('获取园区基础信息列表失败');
  } finally {
    loading.value = false;
  }
};

const foundationDialog = ref();
const handleOpenDialog = (type: string, row?: any) => {
  foundationDialog.value.open(type, row);
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  foundationInfoFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 处理选择变化 */
const handleSelectionChange = (selection: FoundationInfoVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length === 1;
  multiple.value = selection.length === 0;
};

const handleSuccess = async (type: any) => {
  if (type === 'create') {
    await proxy?.$modal.msgSuccess('新增成功');
    await resetQuery();
  } else if (type === 'update') {
    await proxy?.$modal.msgSuccess('修改成功');
    await resetQuery();
  }
};

/** 删除按钮操作 */
const handleDelete = async (row?: FoundationInfoVO) => {
  const _ids = row?.id || ids.value;
  try {
    await proxy?.$modal.confirm(`确定删除所选的信息吗？`);
    await delFoundationInfo(_ids);
    proxy?.$modal.msgSuccess('删除成功');
    await getList();
  } catch (error) {
    // console.error('删除失败', error);
    // proxy?.$modal.msgError('删除失败');
  }
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('zichanguanli/foundationInfo/export', { ...queryParams.value }, `foundationInfo_${new Date().getTime()}.xlsx`);
};

onMounted(() => {
  getList();
});
</script>
